<?php
// +----------------------------------------------------------------------
// | 链乡 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: codeshuhao
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\servlet\BuildServlet;
use think\Exception;

class BuildService extends BaseService
{
    private $servlet = null;

    public function __construct()
    {
        parent::__construct();

        $this->servlet = new BuildServlet();

        if ( config('app_debug') !== true )
        {
            throw new Exception('代码自动工具仅在调试模式可用', 1);
        }
    }

    public function getView()
    {
        $data = [
            'form' => [
                'label' => '新增',
                'checkbox' => true,
            ],
            'edit' => [
                'label' => '编辑',
                'checkbox' => true,
            ],
            'page' => [
                'label' => '列表',
                'checkbox' => true,
            ],
            'delete' => [
                'label' => '删除',
                'checkbox' => true,
            ],
            'detail' => [
                'label' => '详情',
                'checkbox' => false,
            ],
        ];

        return $data;
    }

    /**
     * 构建需创建的目录以及文件和文件模板的数据结构
     * @param array $data
     * @return array|bool
     */
    public function helper($data = [])
    {
        //验证数据是否正确
        $data = $this->servlet->validity($data);
        if ( $data === false )
        {
            $this->error = $this->servlet->getError();

            return false;
        }

        //获取各层目录及文件和数据填充模板
        $data = $this->servlet->inquire($data);


        if ( $data === false )
        {
            $this->error = $this->servlet->getError();

            return false;
        }

        //对即将生成的目录及文件进行校验
        $data = $this->servlet->confirmation($data);

        if( $data === false )
        {
            $this->error = $this->servlet->getError();

            return false;
        }

        //生成文件
        $data = $this->servlet->build($data);

        if( $data === false )
        {
            $this->error = $this->servlet->getError();

            return false;
        }

        return $data;
    }

    /**
     * 创建模块
     * @param array $data
     * @return array|bool
     */
    public function module($data = [])
    {
        $module = lcfirst($data['module']);

        if( is_dir(APPLICATION_PATH.$module) )
        {
            $this->error = '模块已存在!';

            return false;
        }

        //获取模块及各层文件夹的数据结构
        $data = $this->servlet->createModule($module);

        //校验数据结构
        if( $this->servlet->checkModule($data) === false )
        {
            $this->error = $this->servlet->getError();

            return false;
        }

        //提取模板数据
        $data = $this->servlet->getTemplates($data);

        //生成文件
        return $this->servlet->build($data);
    }


}